﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicJournal.Account.AuthorAccount
{
    public partial class ProofPayment : System.Web.UI.Page
    {
        private Guid _publicationId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["id"] != null)
                {
                    
                    _publicationId = Guid.Parse(Request.Params["id"]);
                    ViewState["ArticleId"] = _publicationId;
                    Publication publication = EJournalRepository.Instance.GetPublicationById(_publicationId);
                    if (!string.IsNullOrEmpty(publication.PublicationReceiptScan))
                    {
                        PreviousReceiptText.Text = "Предыдущая оплата: " + publication.PublicationReceiptScan;
                    }
                }
            }
        }

        protected void OkBtn_Click(object sender, EventArgs e)
        {
            var pubId = Guid.Parse(ViewState["ArticleId"].ToString());
            var folderToSaveIn = Server.MapPath(@"..\") + @"\files\publications\";
            EJournalRepository.Instance.SaveRecieptNumberPublications(pubId, UploadFileOnServer(PaymentScanUpload,folderToSaveIn));
            Response.Redirect("MyAccount.aspx");
        }

        private string UploadFileOnServer(FileUpload uploader, string folder)
        {
            //System.IO.Directory.CreateDirectory(folder);
            string FileName = System.IO.Path.GetFileName(uploader.FileName);
            
            if ((uploader.PostedFile != null) && (uploader.PostedFile.ContentLength > 0))
            {
                uploader.PostedFile.SaveAs(folder + FileName);
            }
            return FileName;
        }


        protected void BackBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("MyAccount.aspx");
        }
    }
}